/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package regulatoryInteractionsStats.yeast;

import java.util.HashSet;
import utils.fishersExact.FishersExactTest;

/**
 *
 * @author jamie
 */
public class PPIStats {

    private double prior = 0.5;

    public PPIStats() {
        YeastGeneLibrary yeastGeneLibrary = new YeastGeneLibrary();
        yeastGeneLibrary.printStats();
        PPIInteractionSet ppiInteractionSet = new PPIInteractionSet(yeastGeneLibrary);
        RegultoryInteractionSet regultoryInteractionSet = new RegultoryInteractionSet(yeastGeneLibrary);
        System.out.println("there are "+regultoryInteractionSet.getRegulatoryInteractions().size()+" regulatory interactions");
        System.out.println("there are "+ppiInteractionSet.getPPIInteractions().size()+" ppi interactions");
        HashSet<String> intersection = getIntersection(ppiInteractionSet.getPPIInteractions(), regultoryInteractionSet.getRegulatoryInteractions());
        System.out.println("there are "+intersection.size()+" interactions that are both regulatory and physical");

        int universeSize = getUniverse(ppiInteractionSet, regultoryInteractionSet).size();
        int possibleInteractions = ((universeSize*universeSize)/2)+(universeSize/2);
        FishersExactTest fet = new FishersExactTest(possibleInteractions, regultoryInteractionSet.getRegulatoryInteractions().size(), ppiInteractionSet.getPPIInteractions().size(), intersection.size());
        double top = ((intersection.size()+0.0)/regultoryInteractionSet.getRegulatoryInteractions().size())*prior;
        double x = (ppiInteractionSet.getPPIInteractions().size()-11+0.0)/(possibleInteractions);
        double bottom = top+(0.55*x);
        System.out.println(possibleInteractions);
        System.out.println(universeSize);
        System.out.println(top);
        System.out.println(bottom);
        System.out.println(fet.getResult()+", "+fet.getFoldChange()+", "+(top/bottom));

    }

    private HashSet<String> getIntersection(HashSet<String> set1, HashSet<String> set2) {
        HashSet<String> hs = new HashSet<String>();
        hs.addAll(set1);
        hs.retainAll(set2);
        return hs;
    }

    private int getNoOfAllPossibleInteractions(YeastGeneLibrary yeastGeneLibrary) {
        return yeastGeneLibrary.getAllGenes().size()*yeastGeneLibrary.getAllGenes().size();
    }

    private HashSet<String> getUniverse(PPIInteractionSet ppiInteractionSet, RegultoryInteractionSet regultoryInteractionSet) {
        HashSet<String> all = new HashSet<String>();
        all.addAll(ppiInteractionSet.getGenes());
        for(String symbol : regultoryInteractionSet.getGenes()){
            if(!all.contains(symbol)){
                all.add(symbol);
            }
        }
        return all;

    }



    





}
